﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S3System.DataAccess;

using S3System.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.Unity;
using System.ServiceModel.Activation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.ServiceModel;
using System.Security.Principal;
using System.Security.Permissions;

namespace S3System.BusinessLogic.CRM
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class BCCustomerManagement : IBCCustomerManagement, IBCCustomerQueries
    {        
        [Dependency]
        public ICustomerRepository CustomerRepository { get; set; }

        public  void MaintenanceCustomer(Customer item)
        {            
            try
            {
                this.CustomerRepository.Maintenance(item);            
            }
            catch (Exception ex)
            {            
                bool rethrow = ExceptionPolicy.HandleException(ex, Infrastructure.Aspect.Constants.ExceptionPolicyNames.BusinessLogicPolicy);
                if ( rethrow  )
                    throw;
            }
            
        }

        public IEnumerable<Customer> GetAll()
        {
            return this.CustomerRepository.GetAll(); 
        }

 


        public string QueryCustomers(QueryCustomerOptions options =  QueryCustomerOptions.getAll, string name  =null, DateTime? birthDate =  null )
        {
            string result = null;  
            switch (options)
            {
                case QueryCustomerOptions.getAll:
                    var db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                    var cmd = db.GetSqlStringCommand("set @result = (select  * from customers for xml path('Customer'), root('Customers'))");
                    db.AddOutParameter(cmd, "result", System.Data.DbType.String, int.MaxValue);
                    db.ExecuteNonQuery(cmd);
                    result = db.GetParameterValue(cmd, "result") as string;
                    
                    break;
                case QueryCustomerOptions.getByFirstName:
                    break;
                case QueryCustomerOptions.getByLastName:
                    break;
                case QueryCustomerOptions.getByBrithDate:
                    break;
                default:
                    break;
            }            
            return result; 
        }



        
        [PrincipalPermission(SecurityAction.Demand, Role="Administrators")]
        public string ValidateConfiguration()
        {


            StringBuilder  sb  =  new StringBuilder(); 
            try
            {
                sb.AppendLine("Primary Identity " + OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name);                
            }
            catch (Exception ex)
            {
             
            }
            try
            {
                sb.AppendLine("Windows Identity " + OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name);
            }
            catch (Exception ex)
            {
             
            }
            try
            {
                sb.AppendLine("Windows SO Identity " + WindowsIdentity.GetCurrent().Name);
            }
            catch (Exception ex)
            {

            }
            return sb.ToString();
        
        }

        
    }
}
